package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:链式栈
 * User: LianBao
 * Date: 2024-04-01
 * Time: 15:06
 */
public class myStack2<E> {
    static class ListNode {

        public Object val;//数据
        public ListNode next;//下一个结点

        public ListNode(Object val) {
            this.val = val;
        }
    }

    public ListNode head;//(头结点)第一个结点

    //入栈
    public E push(E val) {
        ListNode newNode = new ListNode(val);
        if (head != null) {
            newNode.next = head;
        }
        head = newNode;
        return val;
    }

    //出栈
    public E pop() {
        if (head == null) {
            return null;
        }
        Object ret = head.val;

        if (head.next == null) {
            head = null;
            return (E) ret;
        }


        head = head.next;


        return (E) ret;
    }

    //获取栈顶元素
    public E peek() {
        if (head == null) {
            return null;
        }
        return (E) head.val;
    }

    //判断栈是否为空
    public boolean empty() {
        if (head == null) {
            return true;
        }
        return true;
    }

}
